package com.blueocean_health.care.management.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.Department;
import com.blueocean_health.care.management.domain.base.NurseWorkerInfo;
import com.blueocean_health.care.management.domain.base.PostageV;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.Team;
import com.blueocean_health.care.management.domain.dto.OrderInfoPriceDto;
import com.blueocean_health.care.management.domain.dto.OrderInfoPriceRespDto;
import com.blueocean_health.care.management.domain.dto.PriceDto;
import com.blueocean_health.care.management.domain.ordervo.PostageVo;
import com.blueocean_health.care.management.em.OrderTimeTypeEnum;
import com.blueocean_health.care.management.em.PayPatternEnum;
import com.blueocean_health.care.management.em.ServiceTypeEnum;
import com.blueocean_health.care.management.exception.OrderServiceException;
import com.blueocean_health.care.management.mapper.DepartmentMapper;
import com.blueocean_health.care.management.mapper.NurseWorkerInfoMapper;
import com.blueocean_health.care.management.mapper.PostageVMapper;
import com.blueocean_health.care.management.mapper.PriceMapper;
import com.blueocean_health.care.management.mapper.TeamMapper;
import com.blueocean_health.care.management.service.OrderInfoPriceService;

@Service
public class OrderInfoPriceServiceImpl implements OrderInfoPriceService {
	@Resource
	private PostageVMapper postageVMapper;
	@Resource
	private DepartmentMapper departmentMapper;
	@Resource
	private TeamMapper teamMapper;
	@Resource
	private NurseWorkerInfoMapper nurseWorkerInfoMapper;
//	@Autowired
//	private PriceMapper priceMapper;
	@Value("${postage.currency}")
	private String currency;
//	@Override
//	public SysResult findPrice(OrderInfoPriceDto orderInfoPriceDto) throws OrderServiceException {
//		if(orderInfoPriceDto == null || StringUtils.isEmpty(orderInfoPriceDto.getIllnessGrade(), orderInfoPriceDto.getServiceType())){
//			throw new OrderServiceException("param error");
//		}
//		List<PriceDto> priceList = priceMapper.findPrice(orderInfoPriceDto);
//		if(priceList == null || priceList.size() == 0) {
//			return SysResult.ok(new OrderInfoPriceRespDto());
//		}
//		for (PriceDto priceDto : priceList) {
//			if(orderInfoPriceDto.getDepartment().equals(priceDto.getDeptId())){
//				return SysResult.ok(new OrderInfoPriceRespDto(priceDto));
//			}
//		}
//
//		//默认
//		//return SysResult.ok(new OrderInfoPriceRespDto(priceList.get(0)));
//
//        //没有选科室的时候，随便给一个科室的价格，因为目前所有的科室的价格都是一样的
//        return SysResult.ok(new OrderInfoPriceRespDto(priceList.get(1)));
//	}
   //检查医院资费模型
	 private boolean checkhospital(String hospitalId){
			Team hospital = teamMapper.selectByPrimaryKey(Integer.valueOf(hospitalId));
			String workerPayPattern = hospital.getWorkerPayPattern();
		return workerPayPattern.equals(PayPatternEnum._1.getKey());
	 }
	   //查看护工资费信息
		 private Double getworkerCommission(String workerId,String hospitalId){
			 NurseWorkerInfo nurseWorkerInfo=new NurseWorkerInfo();
			 nurseWorkerInfo.setWorkerId(workerId);
			 nurseWorkerInfo.setHospital(hospitalId);
			 nurseWorkerInfo.setIsDel(false);
			List<NurseWorkerInfo> select = nurseWorkerInfoMapper.select(nurseWorkerInfo);
			Double commissionRate =0.0;
			if(null!=select&&select.size()>0){
				 commissionRate = select.get(0).getCommissionRate();
				
			}
			
			return commissionRate;
			
		 }
		
	@Override
	public OrderInfoPriceRespDto findPricenew(OrderInfoPriceDto orderInfoPriceDto) throws OrderServiceException{
	//查看医院模型
		boolean checkhospital = checkhospital(orderInfoPriceDto.getHid());
//		if(!checkhospital&&StringUtils.isEmpty(orderInfoPriceDto.getWorkerId())){
//		return new OrderInfoPriceRespDto();
//		};
		//获取护工提成比例
		Double getworkerCommission = getworkerCommission(orderInfoPriceDto.getWorkerId(), orderInfoPriceDto.getHid());
		
		String illnessGrade = orderInfoPriceDto.getIllnessGrade();
		if(orderInfoPriceDto == null || StringUtils.isEmpty(illnessGrade, orderInfoPriceDto.getServiceType())){
			throw new OrderServiceException("param error");
		}
		PostageV postageV=new PostageV();
		postageV.setTimetypeId(orderInfoPriceDto.getTimetypeId());
		String department = orderInfoPriceDto.getDepartment();
		if(StringUtils.isEmpty(department)){
			return new OrderInfoPriceRespDto();
		};
		String nurseManager="";
		Department selectByPrimaryKey = departmentMapper.selectByPrimaryKey(Integer.valueOf(department));
		if(null!=selectByPrimaryKey){
		 nurseManager = selectByPrimaryKey.getNurseManager();
		}
		
		postageV.setDeptId(department);
		orderInfoPriceDto.getIllnessGrade();
		postageV.setIllnessId(illnessGrade);
		postageV.setServiceId(orderInfoPriceDto.getServiceType());
		postageV.setSingleServiceId(orderInfoPriceDto.getSingleService());
		postageV.setHospitalId(orderInfoPriceDto.getHid());
		if(postageV.getServiceId().equals(ServiceTypeEnum._5.getKey())&&StringUtils.isEmpty(postageV.getSingleServiceId())){
			return new OrderInfoPriceRespDto(nurseManager);
		}
//		BeanUtils.copyProperties(postageVVo, postageV);
		String deptId = department;
		//最高优先级别
		List<PostageV> select1 = postageVMapper.selectForlist(postageV);
		if(null!=select1&&select1.size()>0){
			return getReturnPrice(select1.get(0),nurseManager,checkhospital,getworkerCommission,orderInfoPriceDto.getTimetypeId());
		}
		//第三优先级别
//		postageV.setDeptId(deptId);
		postageV.setIllnessId(currency);
		List<PostageV> select3 = postageVMapper.selectForlist(postageV);
		if(null!=select3&&select3.size()>0){
			return getReturnPrice(select3.get(0),nurseManager,checkhospital,getworkerCommission,orderInfoPriceDto.getTimetypeId());
		}	
		//第二优先级别
		postageV.setIllnessId(illnessGrade);
		postageV.setDeptId(currency);
		List<PostageV> select2 = postageVMapper.selectForlist(postageV);
		if(null!=select2&&select2.size()>0){
				return getReturnPrice(select2.get(0),nurseManager,checkhospital,getworkerCommission,orderInfoPriceDto.getTimetypeId());
		}
		//第四优先级别
		postageV.setIllnessId(currency);
		postageV.setDeptId(currency);
		List<PostageV> select4 = postageVMapper.selectForlist(postageV);
		if(null!=select4&&select4.size()>0){
				return getReturnPrice(select4.get(0),nurseManager,checkhospital,getworkerCommission,orderInfoPriceDto.getTimetypeId());
			}	
				
		return new OrderInfoPriceRespDto(nurseManager);
	}
	//构建出参对象
	private OrderInfoPriceRespDto getReturnPrice(PostageV postageV,String nurseManager){
		OrderInfoPriceRespDto OrderInfoPriceRespDt=new OrderInfoPriceRespDto();
		OrderInfoPriceRespDt.setHourPay(postageV.getNurseHourPay());
		OrderInfoPriceRespDt.setHourPrice(postageV.getOneHourPay());
		OrderInfoPriceRespDt.setOneDayPay(postageV.getNurseDayPay());
		OrderInfoPriceRespDt.setOneDayPrice(postageV.getOneDayPay());
		OrderInfoPriceRespDt.setHeadNurse(nurseManager);
		return OrderInfoPriceRespDt;
		
	}
	//构建出参对象(根据出参对象)
	private OrderInfoPriceRespDto getReturnPrice(PostageV postageV,String nurseManager,boolean checkhospital,Double getworkerCommission,String timeType){
		OrderInfoPriceRespDto OrderInfoPriceRespDt=new OrderInfoPriceRespDto();
		
		OrderInfoPriceRespDt.setHourPay(checkhospital?postageV.getNurseHourPay():postageV.getOneHourPay()*getworkerCommission/100);
		OrderInfoPriceRespDt.setHourPrice(postageV.getOneHourPay());
		OrderInfoPriceRespDt.setOneDayPay(checkhospital?postageV.getNurseDayPay():postageV.getOneDayPay()*getworkerCommission/100);
		OrderInfoPriceRespDt.setOneDayPrice(postageV.getOneDayPay());
		OrderInfoPriceRespDt.setHeadNurse(nurseManager);
//		if(OrderInfoPriceRespDt.getHourPay()==0&&timeType.equals(OrderTimeTypeEnum._1.getKey()+"")){
//			OrderInfoPriceRespDt.setHourPay(OrderInfoPriceRespDt.getOneDayPay()/24);
//		}
//		if(OrderInfoPriceRespDt.getHourPay()==0&&timeType.equals(OrderTimeTypeEnum._2.getKey()+"")){
//			OrderInfoPriceRespDt.setHourPay(OrderInfoPriceRespDt.getOneDayPay()/12);
//		}
//		if(OrderInfoPriceRespDt.getHourPrice()==0&&timeType.equals(OrderTimeTypeEnum._1.getKey()+"")){
//			OrderInfoPriceRespDt.setHourPrice(OrderInfoPriceRespDt.getOneDayPrice()/24);
//		}
//		if(OrderInfoPriceRespDt.getHourPrice()==0&&timeType.equals(OrderTimeTypeEnum._2.getKey()+"")){
//			OrderInfoPriceRespDt.setHourPrice(OrderInfoPriceRespDt.getOneDayPrice()/12);
//		}
		return OrderInfoPriceRespDt;
		
	}
}
